package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.TicketTrans;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TicketCustomer;
import com.ruoyi.system.service.ITicketCustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户信息Controller
 *
 * @author ruoyi
 * @date 2024-07-15
 */
@RestController
@RequestMapping("/system/customer")
public class TicketCustomerController extends BaseController
{
    @Autowired
    private ITicketCustomerService ticketCustomerService;

    /**
     * 查询客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(TicketCustomer ticketCustomer)
    {
        startPage();
        List<TicketCustomer> list = ticketCustomerService.selectTicketCustomerList(ticketCustomer);
        return getDataTable(list);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TicketCustomer> util = new ExcelUtil<TicketCustomer>(TicketCustomer.class);
        util.importTemplateExcel(response, "客户数据");
    }

    /**
     * 导入客户信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer:import')")
    @Log(title = "客户信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TicketCustomer> util = new ExcelUtil<TicketCustomer>(TicketCustomer.class);
        List<TicketCustomer> customerList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ticketCustomerService.importCustomer(customerList, updateSupport, operName);
        return success(message);
    }


    /**
     * 导出客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:customer:export')")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TicketCustomer ticketCustomer)
    {
        List<TicketCustomer> list = ticketCustomerService.selectTicketCustomerList(ticketCustomer);
        ExcelUtil<TicketCustomer> util = new ExcelUtil<TicketCustomer>(TicketCustomer.class);
        util.exportExcel(response, list, "客户信息数据");
    }

    /**
     * 获取客户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer:query')")
    @GetMapping(value = "/{customerNo}")
    public AjaxResult getInfo(@PathVariable("customerNo") String customerNo)
    {
        return success(ticketCustomerService.selectTicketCustomerByCustomerNo(customerNo));
    }

    /**
     * 新增客户信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer:add')")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TicketCustomer ticketCustomer)
    {
        ticketCustomer.setCreateBy(getUsername());
        return toAjax(ticketCustomerService.insertTicketCustomer(ticketCustomer));
    }

    /**
     * 修改客户信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer:edit')")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TicketCustomer ticketCustomer)
    {
        ticketCustomer.setUpdateBy(getUsername());
        return toAjax(ticketCustomerService.updateTicketCustomer(ticketCustomer));
    }

    /**
     * 删除客户信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer:remove')")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerNos}")
    public AjaxResult remove(@PathVariable String[] customerNos)
    {
        return toAjax(ticketCustomerService.deleteTicketCustomerByCustomerNos(customerNos));
    }
}
